package com.hx.controller;

import com.alibaba.excel.EasyExcel;
import com.hx.entry.CangKuImportDTO;
import com.hx.listener.EnhancedCangKuListener;
import com.hx.service.ICangKuService;
import com.hx.service.impl.CangKuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/test")
@RestController
public class DataImportController {
    @Autowired
    private ICangKuService cangKuService;

    @PostMapping("/import")
    public ResponseEntity<?> importData(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), CangKuImportDTO.class,
                            new EnhancedCangKuListener(cangKuService))
                    .sheet()
                    .doRead();

            return ResponseEntity.ok("导入请求已接受");
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("文件读取失败：" + e.getMessage());
        }
    }
}